/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.faesa.acervo.view.mbeans;

import br.faesa.acervo.services.ws.AcervoWebService;
import br.faesa.acervo.services.ws.AcervoWebService_Service;
import br.faesa.acervo.services.ws.Emprestimo;
import br.faesa.acervo.services.ws.Livro;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author aluno.faesa
 */
@ManagedBean(name = "emprestimoExclusaoForm")
@ViewScoped
public class EmprestimoExclusaoFormBean implements Serializable {

    private Long matricula;
    
    private Long idLivro;
    
    @Resource(mappedName = "jms/ReservaQueueFactory")
    private QueueConnectionFactory queueConnectionFactory;
    
    @Resource(mappedName = "jms/ReservaQueue")
    private Queue queue;
    
    public EmprestimoExclusaoFormBean() { }
    
    @PostConstruct
    public void init()
    {
        
        final String SELECTED_ID = "idLivro";
        
        Map<String, String> paramMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        
        
        if(paramMap.get(SELECTED_ID) != null){
            
            idLivro = Long.valueOf(paramMap.get(SELECTED_ID));
            
        }else{
            FacesUtils.fatal("Acesse a página a partir da seleção de um Livro!");
        }

    }
    
    public void onBotaoOKClick()
    {
    
        HttpServletRequest http = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Emprestimo emp = (Emprestimo) http.getAttribute("emp");

        try {
        
            System.out.println("-----------------AQUI--------------" + emp.getId());
            String message = emp.getId().toString();
            Connection connection = queueConnectionFactory.createConnection();
            
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = (MessageProducer)
                    session.createProducer(queue);
            TextMessage textMessage = session.createTextMessage();
            textMessage.setStringProperty("idEmprestimo", message);
            messageProducer.send(textMessage);
        
        } catch (JMSException ex) {
            Logger.getLogger("ReservaLog").log(Level.WARNING,
                    "JMSException",
                    "JMSException");
        }
        
        FacesUtils.info("Reserva Excluída com sucesso!");
    }
    
    public Collection<br.faesa.acervo.services.ws.Emprestimo> getEmprestimosAgendados(){
        AcervoWebService_Service aws = new AcervoWebService_Service();
        
        AcervoWebService service = aws.getAcervoWebServicePort();
        
        Livro livro = new Livro();
        livro.setId(idLivro);
        
        return service.obterAgendamentosReservas(livro);
    }


    public Long getMatricula() {
        return matricula;
    }

    public void setMatricula(Long matricula) {
        this.matricula = matricula;
    }

}
